package net.xdclass.component;


import net.xdclass.utils.CommonUtil;
import org.springframework.stereotype.Component;

@Component
public class ShortLinkComponent {
    private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    /**
     * 生成短链码
     * @param param
     * @return
     */
    public String  createShortLinkCode(String param) {
        Long murmurHash32 = CommonUtil.murmurHash32(param);
        //进制转换
        String code = encodeToBase62(murmurHash32);
        return code;
    }


    /**
     * 10转62进制
     * @param num
     * @return
     */
    private String encodeToBase62(long num) {
        StringBuffer stringBuffer = new StringBuffer();
        do {
        int i= (int)(num % 62);
        stringBuffer.append(CHARS.charAt(i));
        num = num / 62;
        }while (num > 0);
        String value =  stringBuffer.reverse().toString();
        return value;

    }

}
